<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use App\Models\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id','序号')->sortable();
            $grid->column('category_name','所属类别')->label('#586cb1');
            $grid->column('title','文章标题');
            $grid->column('describe','文章简介')->limit(18);
            $grid->column('pv','访问量');
            $grid->column('created_at','添加时间');
            $grid->column('updated_at','更新时间')->sortable();
            // 禁用过滤器按钮
            //$grid->disableFilterButton();
            $grid->disableViewButton();

//            $grid->selector(function (Grid\Tools\Selector $selector) {
//                $category = Category::all('id','name')->toArray();
//                $category = array_column($category,'name','id');
//                $selector->select('category_id', '类别', $category);
//            });

            $grid->quickSearch(function ($model, $query) {
                $model->where('title', $query);
            })->placeholder('文章标题搜索...');

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                // 展开过滤器
                $filter->expand();

                $filter->equal('id','文章id')->width(1.5);
                $filter->like('title','文章标题')->width(3);
                $category = Category::all('id','name')->toArray();
                $category = array_column($category,'name','id');
                $filter->equal('category_id','类别名称')->select($category)->width(2);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id','序号');
//            $show->field('category_id');
            $show->field('category_name','所属类别');
            $show->field('title','文章标题');
            $show->field('describe','文章简介');
            $show->field('content','文章内容');
            $show->field('pv','访问量');
            $show->field('updated_at','更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        //查询所有类别
        $categorys = Category::all('id','name as text')->toArray();
        $categorys = array_column($categorys,'text','id');
        return Form::make(new Article(), function (Form $form) use ($categorys) {


            $form->saving(function (Form $form) {

                //将文章内容中的图片提取出来用于展示
                $content =  $form->content;
                $images= getImages($content);
                $form->images = implode(',',$images);
                //新增
                if ($form->isCreating()) {
                    $form->created_at = date('Y-m-d H:i:s');
                    $form->updated_at = date('Y-m-d H:i:s');
                }
                //编辑
                if ($form->isEditing()) {
                    $form->updated_at = date('Y-m-d H:i:s');
                }

            });


            $form->submitted(function (Form $form) {

                $category_id = $form->category_id;
                $category = Category::where('id',$category_id)->first();
                if($category){
                    $form->category_id = $category_id;
                    $form->category_name = $category->name;
                }

            });





            $form->display('id');
            $form->hidden('category_name');
            $form->hidden('images');
            $form->select('category_id','所属类别')->options($categorys);
            $form->text('title','文章标题');
            $form->image('main_image','文章主图');
            $form->textarea('describe','文章简介');
            $form->editor('content','文章内容');
            $form->text('pv','访问量')->default(0);

            $form->display('created_at','添加时间');
            $form->display('updated_at','更新时间');

            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            $form->disableViewCheck();
            $form->disableResetButton();

        });
    }
}
